package com.haole.ecommercepay.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author haole
 */
public class DigestUtils {

    private static final Logger logger = LoggerFactory.getLogger(DigestUtils.class);

    /**
     * 计算字符串Sha1
     * @param data
     * @return
     */
    public static String getStrSha1(String data) {
        String sha1 = "";
        try {
            MessageDigest md = MessageDigest.getInstance("SHA1");
            md.update(data.getBytes());
            StringBuffer buf = new StringBuffer();
            byte[] bits = md.digest();
            for (int i = 0; i < bits.length; i++) {
                int a = bits[i];
                if (a < 0) a += 256;
                if (a < 16) buf.append("0");
                buf.append(Integer.toHexString(a));
            }
            sha1 = buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return sha1;
    }

    /**
     * 计算字符串MD5
     * @param data
     * @return
     */
    public static String getStrMd5(String data) {
        String md5 = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(data.getBytes());
            StringBuffer buf = new StringBuffer();
            byte[] bits = md.digest();
            for (int i = 0; i < bits.length; i++) {
                int a = bits[i];
                if (a < 0) a += 256;
                if (a < 16) buf.append("0");
                buf.append(Integer.toHexString(a));
            }
            md5 = buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return md5;
    }

    /**
     * 计算文件Sha1
     * @param filePath
     * @return
     */
    public static String getFileSha1(String filePath) {
        File file = new File(filePath);
        FileInputStream in = null;
        String sha1 = "";
        try {
            in = new FileInputStream(file);
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            byte[] buffer = new byte[1024 * 1024 * 10];

            int len = 0;
            while ((len = in.read(buffer)) > 0) {
                digest.update(buffer, 0, len);
            }
            sha1 = new BigInteger(1, digest.digest()).toString(16);
            int length = 40 - sha1.length();
            if (length > 0) {
                for (int i = 0; i < length; i++) {
                    sha1 = "0" + sha1;
                }
            }
        } catch (IOException e) {
            logger.error("IOException：{}", e);
        } catch (NoSuchAlgorithmException e) {
            logger.error("NoSuchAlgorithmException：{}", e);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                logger.error("IOException：{}", e);
            }
        }
        return sha1;
    }

    /**
     * 计算文件MD5
     * @param filePath
     * @return
     */
    public static String getFileMD5(String filePath) {
        File file = new File(filePath);
        FileInputStream in = null;
        String md5 = "";
        try {
            in = new FileInputStream(file);
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] buffer = new byte[1024 * 1024 * 10];

            int len = 0;
            while ((len = in.read(buffer)) > 0) {
                digest.update(buffer, 0, len);
            }
            md5 = new BigInteger(1, digest.digest()).toString(16);
            int length = 32 - md5.length();
            if (length > 0) {
                for (int i = 0; i < length; i++) {
                    md5 = "0" + md5;
                }
            }
        } catch (IOException e) {
            logger.error("IOException：{}", e);
        } catch (NoSuchAlgorithmException e) {
            logger.error("NoSuchAlgorithmException：{}", e);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                logger.error("IOException：{}", e);
            }
        }
        return md5;
    }
}
